package com.q.tqb.fragment;


import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.q.tqb.R;
import com.q.tqb.activity.mine.MineBusinessActivity;
import com.q.tqb.base.AppConst;
import com.q.tqb.model.ShareInfo;
import com.q.tqb.util.DensityUtil;
import com.q.tqb.util.DeviceUtil;
import com.q.tqb.util.SPUtil;
import com.q.tqb.view.popwindow.SharePopWindow;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class TweeterShareFragment extends Fragment {

    @BindView(R.id.iv_code_bg)
    ImageView mIvCodeBg;

    private Bitmap mBgBitmap;
    private Bitmap mBitmap;
    private String headUrl;
    private int uid;
    public boolean isShare;

    Unbinder unbinder;
    private View view;


    public TweeterShareFragment() {
        // Required empty public constructor
    }

    public static TweeterShareFragment newInstance() {
        TweeterShareFragment fragment = new TweeterShareFragment();
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_tweeter_share, container, false);
        unbinder = ButterKnife.bind(this, view);
        headUrl = SPUtil.getString(getActivity(), "header");
        uid = (int) SPUtil.get(getActivity(), "uid", 0);
        loadImageSimpleTarget();
        return view;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private SimpleTarget<Bitmap> target = new SimpleTarget<Bitmap>(250, 250) {
        @Override
        public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) {
            //这里我们拿到回掉回来的bitmap，可以加载到我们想使用到的地方
            mBgBitmap = ((BitmapDrawable) mIvCodeBg.getDrawable()).getBitmap();
            mBitmap = CodeUtils.createImage(AppConst.URL_CODE_SHARE + uid, DensityUtil.dip2px(getActivity(), 120), DensityUtil.dip2px(getActivity(), 120), bitmap);
            mBgBitmap = createWaterMaskCenter(mBgBitmap, mBitmap);
            mIvCodeBg.setImageBitmap(mBgBitmap);
            isShare = true;
        }

    };

    private void loadImageSimpleTarget() {
        Glide
                .with(getActivity())
                .load(headUrl)
                .asBitmap()
                .into(target);
    }


    public static Bitmap createWaterMaskCenter(Bitmap src, Bitmap watermark) {
        return createWaterMaskBitmap(src, watermark, (src.getWidth() - watermark.getWidth()) / 2, (src.getHeight() - watermark.getHeight()) / 2);
    }

    private static Bitmap createWaterMaskBitmap(Bitmap src, Bitmap watermark,
                                                int paddingLeft, int paddingTop) {
        if (src == null) {
            return null;
        }
        int width = src.getWidth();
        int height = src.getHeight();
        // 创建一个新的和SRC长度宽度一样的位图
        Bitmap newb = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        //将该图片作为画布
        Canvas canvas = new Canvas(newb);
        //在画布 0，0坐标上开始绘制原始图片
        canvas.drawBitmap(src, 0, 0, null);
        //在画布上绘制水印图片
        canvas.drawBitmap(watermark, paddingLeft, paddingTop, null);
        // 保存
        canvas.save(Canvas.ALL_SAVE_FLAG);
        // 存储
        canvas.restore();
        return newb;
    }

    public void share() {
        ShareInfo shareInfo = new ShareInfo();
        shareInfo.setType(1);
        SharePopWindow sharePopWindow = new SharePopWindow(getActivity(), shareInfo, mBgBitmap);
        sharePopWindow.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }


}
